//package com.popay.common.utils;
//
//import com.google.common.util.concurrent.ThreadFactoryBuilder;
//import org.apache.commons.lang3.time.DateFormatUtils;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//
//import java.util.Date;
//import java.util.concurrent.ExecutorService;
//import java.util.concurrent.LinkedBlockingQueue;
//import java.util.concurrent.ThreadPoolExecutor;
//import java.util.concurrent.TimeUnit;
//
///**
// * @version v1.0
// * @ProjectName: p-springcloud
// * @ClassName: DingdingMonitor
// * @Description: TODO
// * @Author: 晨曦
// * @Date: 2021-6-23 10:25
// */
//public class DingdingMonitor {
//
//    private static final Logger logger = LoggerFactory.getLogger(DingdingMonitor.class);
//
//    //注意该token不可泄漏
//    private static String DING_URL = "https://oapi.dingtalk.com/robot/send?access_token=b30dbce9aada37d948ecefdbd1a87957210139e8f0bbe85456ad5a33cbf65a44";
//
//    private static final ExecutorService ALARM_POOL =
//            new ThreadPoolExecutor(2,
//                    2,
//                    0L,
//                    TimeUnit.MILLISECONDS,
//                    new LinkedBlockingQueue<>(10),
//                    (new ThreadFactoryBuilder()).setNameFormat("DINGDING_THREAD-%d").build(),
//                    //丢弃
//                    new ThreadPoolExecutor.DiscardPolicy()
//            );
//
//    public static void sendAlarmInfo(EnumErrorType errorType, String errorMsg) {
//        ALARM_POOL.execute(() -> {
//            CommonMetricsMonitor.errorCounter.labels("DING", "ERROR", "dingding").inc();
//            logger.info("啥内容:{},{}", errorType, errorMsg);
//            sendMsg(errorType, errorMsg, null);
//        });
//
//    }
//
//    public static void sendAlarmInfo(EnumErrorType errorType, String errorMsg, String remark) {
//        ALARM_POOL.execute(() -> {
//            sendMsg(errorType, errorMsg, remark);
//            logger.info("啥内容:{},{},{}", errorType, errorMsg, remark);
//            CommonMetricsMonitor.errorCounter.labels("DING", "ERROR", "dingding").inc();
//        });
//
//    }
//
//    private static void sendMsg(EnumErrorType errorType, String errorMsg, String remark) {
//
//        if (!PropertyUtil.SEND_MONITOR_STATUS) {
//            return;
//        }
//
//        if (errorType == null || errorMsg == null) {
//            logger.warn("报警类型和消息不能为空");
//            return;
//        }
//        if (StringUtils.isNotBlank(errorMsg)) {
//            errorMsg = errorMsg.replaceAll("\n", "");
//        }
//
//        String errorTypeTitle = errorType.getName();
//
//        try {
//            String warnDate = DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss");
//            DingTalkClient client = new DefaultDingTalkClient(DING_URL);
//            OapiRobotSendRequest request = new OapiRobotSendRequest();
//            request.setMsgtype("markdown");
//            OapiRobotSendRequest.Markdown markdown = new OapiRobotSendRequest.Markdown();
//            markdown.setTitle("用户中心报警");
//            String msg = "#### 服务报警\n"
//                    + "- 告警类型: " + errorTypeTitle + "\n"
//                    + "- 告警主机: " + IpUtil.getIp() + "\n"
//                    + "- 项目名称: 用户中心\n"
//                    + "- 告警时间: " + warnDate + "\n"
//                    + "- 错误信息: **" + errorMsg + "**";
//            if (StringUtils.isNotBlank(remark)) {
//                msg = msg + "\n- " + remark;
//            }
//
//            markdown.setText(msg);
//            request.setMarkdown(markdown);
//            OapiRobotSendResponse response = client.execute(request);
//        } catch (Exception exception) {
//            logger.warn("发送钉钉消息失败", exception);
//        }
//
//    }
//}
